<?php
/**
 * 帮助类，一些需要用到的公共函数
 * User: yuanji
 * Date: 2018/7/8
 * Time: 16:01
 */

namespace Lib;

class Utol
{
    /**
     * 通过ip段获取ip列表
     * @param $startIp
     * @param $endIp
     * @return array
     * @throws \Exception
     */
    public static function getIps($startIp, $endIp)
    {
        if (filter_var($startIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) == FALSE) {
            throw new \Exception("{$startIp} 不是一个有效的IPV4地址，请修改后重试");
        }
        if (filter_var($endIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) == FALSE) {
            throw new \Exception("{$endIp} 不是一个有效的IPV4地址，请修改后重试");
        }

        $ips_int = ip2long($startIp);
        $ipe_int = ip2long($endIp);
        if ($ips_int >= $ipe_int) {
            throw new \Exception("{$startIp}大于等于{$endIp}，请修改后重试!");
        }
        $ips = [];
        for ($i = $ips_int; $i <= $ipe_int; $i++) {
            $ips[] = long2ip($i);
        }
        return $ips;
    }

    /**
     * 获取客户端ip
     * @return array|false|string
     */
    public static function getClientIp()
    {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
            $ip = getenv("REMOTE_ADDR");
        else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
            $ip = $_SERVER['REMOTE_ADDR'];
        else
            $ip = "unknown";
        return ($ip);
    }

    /**
     * 向钉钉机器人发送文本消息
     * @param $post_string
     * @param string $dingtalk_robot_access_token
     * @return array|mixed
     */
    public static function dingtalkRobotSend($post_string, $dingtalk_robot_access_token = 'DINGTALK_ROBOT_ACCESS_TOKEN_DEFAULT')
    {
        $remote_server = "https://oapi.dingtalk.com/robot/send?access_token=" . Config::ini($dingtalk_robot_access_token, '60f41499561e6842a7450180b19fd51a01c34e9b9cb7c1f3729084003e0e8127');
        $data = array('msgtype' => 'text', 'text' => array('content' => date('Y-m-d H:i:s') . "\n" . $post_string));
        $post_string = json_encode($data);
        //$result = request_by_curl($webhook, $data_string);
        //echo $result;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
        // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    /**
     * 将字符串转换为数组
     *
     * @param string $data 字符串
     * @return    array    返回数组格式，如果，data为空，则返回空数组
     */
    public static function string2array($data)
    {
        $data = trim($data);
        if ($data == '') return array();
        if (strpos($data, 'array') === 0) {
            preg_match_all("#'(.*?)'#i", $data, $matches);//正则表达式匹配
            $array = $matches[1];
        } else {
            if (strpos($data, '{\\') === 0) $data = stripslashes($data);
            $array = json_decode($data, true);
            /*if (strtolower(CHARSET) == 'gbk') {
                $array = mult_iconv("UTF-8", "GBK//IGNORE", $array);
            }*/
        }
        return $array;
    }

    /**
     * 数字金额转换成中文大写金额的函数
     *
     * $money = 68000000.00;
     *  echo num_to_rmb($money); //陆仟捌佰万元整
     * String Int $num 要转换的小写数字或小写字符串
     * return 大写数字
     */
    public function num_to_rmb($num)
    {
        $c1 = "零壹贰叁肆伍陆柒捌玖";
        $c2 = "分角元拾佰仟万拾佰仟亿";
        //精确到分后面就不要了，所以只留两个小数位
        $num = round($num, 2);
        //将数字转化为整数
        $num = $num * 100;
        if (strlen($num) > 10) {
            return "金额太大，请检查";
        }
        $i = 0;
        $c = "";
        while (1) {
            if ($i == 0) {
            //获取最后一位数字
                $n = substr($num, strlen($num) - 1, 1);
            } else {
                $n = $num % 10;
            }
            //每次将最后一位数字转化为中文
            $p1 = substr($c1, 3 * $n, 3);
            $p2 = substr($c2, 3 * $i, 3);
            if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
                $c = $p1 . $p2 . $c;
            } else {
                $c = $p1 . $c;
            }
            $i = $i + 1;
            //去掉数字最后一位了
            $num = $num / 10;
            $num = (int)$num;
            //结束循环
            if ($num == 0) {
                break;
            }
        }
        $j = 0;
        $slen = strlen($c);
        while ($j < $slen) {
            //utf8一个汉字相当3个字符
            $m = substr($c, $j, 6);
            //处理数字中很多0的情况,每次循环去掉一个汉字“零”
            if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
                $left = substr($c, 0, $j);
                $right = substr($c, $j + 3);
                $c = $left . $right;
                $j = $j - 3;
                $slen = $slen - 3;
            }
            $j = $j + 3;
        }
        //这个是为了去掉类似23.0中最后一个“零”字
        if (substr($c, strlen($c) - 3, 3) == '零') {
            $c = substr($c, 0, strlen($c) - 3);
        }
        //将处理的汉字加上“整”
        if (empty($c)) {
            return "零元整";
        } else {
            return $c . "整";
        }
    }

}